package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.service.ReportService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;

@RestController
@RequestMapping("/members")
public class MembersController {
    @Reference
    private ReportService reportService;

    @Reference
    private MemberService memberService;

    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody Member member) {
        try {
            memberService.add(member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }
        return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = memberService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
        return pageResult;
    }

    //根据id查询会员信息
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Member member = memberService.findById(id);
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS, member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MEMBER_FAIL);
        }
    }

    //编辑会员信息
    @RequestMapping("/edit")
    public Result edit(@RequestBody Member member) {
        try {
            memberService.edit(member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
    }

    //删除会员
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            memberService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MEMBER_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
    }

    //
    @RequestMapping("/deleteMembersByIds")
    public Result deleteMembersByIds(@RequestBody Integer[] params){
        System.out.println(params);
        try {
            memberService.deleteMembersByIds(params);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MEMBER_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
    }

    //导出Excel报表
    @RequestMapping("/export")
    public Result export(HttpServletRequest request, HttpServletResponse response,Integer[]  params) {
        try {


            //获得Excel模板文件绝对路径
            String temlateRealPath = request.getSession().getServletContext().getRealPath("template") + File.separator + "member_template.xlsx";
            System.out.println(temlateRealPath);
            //读取模板文件创建Excel表格对象
            XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(temlateRealPath)));
            System.out.println("workbook:"+workbook);
            //读取第一个工作表
            XSSFSheet sheet = workbook.getSheetAt(0);
            System.out.println("sheet:"+sheet);
            //读取第一个工作表的指定行
            XSSFRow row = sheet.getRow(1);

            //取出数据,将数据写入到Excel文件中
            Integer memberId;
            for (int i = 0; i < params.length; i++) {
                memberId = params[i];
                Member member = memberService.findById(memberId);

                String fileNumber = member.getFileNumber();
                String name = member.getName();
                String sex = member.getSex();
                String idCard = member.getIdCard();
                String phoneNumber = member.getPhoneNumber();
                Date regTime = member.getRegTime();
                String email = member.getEmail();
                Date birthday = member.getBirthday();

                row = sheet.getRow(i+1);
                row.getCell(0).setCellValue(i+1);
                row.getCell(1).setCellValue(fileNumber);
                row.getCell(2).setCellValue(name);
                row.getCell(3).setCellValue(sex);
                row.getCell(4).setCellValue(idCard);
                row.getCell(5).setCellValue(phoneNumber);
                row.getCell(6).setCellValue(regTime);
                row.getCell(7).setCellValue(email);
                row.getCell(8).setCellValue(birthday);


            }


            //通过输出流进行文件下载
            OutputStream out = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("content-Disposition", "attachment;filename=memberReport.xlsx");
            workbook.write(out);

            out.flush();
            out.close();
            workbook.close();
            //return new Result(true,MessageConstant.EXPORT_SUCCESS);
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            //return new Result(false,MessageConstant.EXPORT_FAIL);
            return null;
        }
    }
}
